﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CommonMvc.Common;
using CommonMvc.Filters;
using CommonMvc.Models;
using CommonMvc.Models.Concreate;
using CommonMvc.Models.Sys;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Distributed;

namespace CommonMvc.Controllers
{
    [TypeFilter(typeof(BaseAuthorizationFilter))]
    public class MenuController : BaseController
    {
        public MenuController(DataContext context, IWorkContext workContext, IDistributedCache distributedCache)
          : base(context, workContext, distributedCache)//依赖注入得到实例  
        { }
        public IActionResult Index()
        {          
            return View();
        }
        [HttpPost]
        public IActionResult MenuList([FromBody]PageModel m)
        {
            List<Menu> list = _dataBaseContext.Menus.Skip((m.CurrentPage - 1) * m.PageSize).Take(m.PageSize).ToList();
            return JsonSuccessResult(new BaseListResult { Page = m, Data = list, ResultCode = 0 });
        }
        public IActionResult Add()
        {
            return View();
        }
        [HttpPost]
        public IActionResult Add(Menu m) {
            _dataBaseContext.Menus.Add(m);
            _dataBaseContext.SaveChanges();
            return JsonSuccessResult(new BaseResult{ ResultCode = 0});
        }
        [HttpPost]
        public IActionResult Edit(Menu m)
        {
            _dataBaseContext.Menus.Add(m);
            _dataBaseContext.SaveChanges();
            return JsonSuccessResult(new BaseResult { ResultCode = 0 });
        }
        public IActionResult Delete(IdModel m) {
            _dataBaseContext.Menus.Remove(_dataBaseContext.Menus.Find(m.Id));
            return JsonSuccessResult(new BaseResult { ResultCode = 0 });

        }




    }
}